var { timeUtils, logUtils } = require("../utils/index");
function Action(name, config) {
  this.name = name;
  this.maxAllowRunHour = config.maxAllowRunHour;
  this.minAllowRunHour = config.minAllowRunHour;
  this.maxRunSCount = config.maxRunSCount;
  this.maxRunFCount = config.maxRunFCount;
  this.interval = config.interval;
  this.todayIsOver = false;
  Object.defineProperty(this, "runSCount", {
    get() {
      return this.storage.get(this.name + "runSCount") || 0;
    },
    set(value) {
      this.storage.put(this.name + "runSCount", value);
    },
  });
  Object.defineProperty(this, "runFCount", {
    get() {
      return this.storage.get(this.name + "runFCount") || 0;
    },
    set(value) {
      this.storage.put(this.name + "runFCount", value);
    },
  });
}
Action.prototype.setStorage = function (storage) {
  this.storage = storage;
};
Action.prototype.goStartPage = function () {
  return true;
};
Action.prototype.beforeExec = function () {
  return true;
};
Action.prototype.exec = function () {
  throw new Error("Action.exec must be implemented");
};
Action.prototype.afterExec = function () {
  this.lastEndTime = Date.now();
};
Action.prototype.canFlow = function () {
  if (!this.checkAllowRunHour()) {
    return;
  }
  if (!this.checkRunCount()) {
    return;
  }
  if (!this.checkInterval()) {
    return;
  }
  if (this.todayIsOver) {
    return;
  }
  return true;
};
Action.prototype.flow = function () {
  // console.show();
  if (!this.canFlow()) {
    // console.hide();
    return;
  }
  if (this.goStartPage()) {
    var beforeExecResult = this.beforeExec();
    if (beforeExecResult) {
      logUtils.log(this.name + "开始执行");
      this.exec();
      this.afterExec();
      logUtils.log(this.name + "结束执行");
    } else {
      logUtils.info(this.name, "beforeExec", "执行失败");
      logUtils.log(this.name + " beforeExec 执行失败");
    }
  } else {
    logUtils.info(this.name, "goStartPage", "执行失败");
    logUtils.log(this.name + " goStartPage 执行失败");
  }
  // console.hide();
};
Action.prototype.checkInterval = function () {
  if (this.interval && this.lastEndTime) {
    var hasExec = timeUtils.hasExec(this.lastEndTime);
    if (hasExec < this.interval) {
      logUtils.info(this.name, "间隔时间未到", hasExec, "/", this.interval);
      return false;
    }
  }
  return true;
};
Action.prototype.checkAllowRunHour = function () {
  var hour = new Date().getHours();
  if (this.maxAllowRunHour) {
    if (hour >= this.maxAllowRunHour) {
      logUtils.info(
        this.name,
        hour,
        "|",
        maxAllowRunHour,
        "最大允许执行时间已过"
      );
      return false;
    }
  }
  if (this.minAllowRunHour) {
    if (hour < this.minAllowRunHour) {
      logUtils.info(
        this.name,
        hour,
        "|",
        minAllowRunHour,
        "最小允许执行时间未到"
      );
      return false;
    }
  }
  return true;
};
Action.prototype.checkRunCount = function () {
  if (this.maxRunSCount) {
    if (this.runSCount >= this.maxRunSCount) {
      logUtils.info(
        this.name,
        this.runSCount,
        "|",
        this.maxRunSCount,
        "执行成功次数超过限制"
      );
      return false;
    }
  }
  if (this.maxRunFCount) {
    if (this.runFCount >= this.maxRunFCount) {
      logUtils.info(
        this.name,
        this.runFCount,
        "|",
        this.maxRunFCount,
        "执行失败次数超过限制"
      );
      return false;
    }
  }
  return true;
};

module.exports = Action;
